package com.etc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.Message;
import com.etc.util.RegexUtil;
import com.etc.util.TelValidator;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@Controller
@RequestMapping("login")
public class LoginAndRegister {
    @Autowired
    private UserService us;
    @Autowired
    private Message msg;
    private Logger log = Logger.getLogger(UserController.class);

//    /**
//     * 史泽浩
//     * 后台登出方法
//     *
//     * @param request
//     * @return
//     */
//    @RequestMapping("loginout")
//    public String loginout(HttpServletRequest request) {
//        HttpSession session = request.getSession();
//        if (session.getAttribute("userInfo") != null) {
//            session.invalidate();
//        }
//        return "redirect:/admin/views/login.jsp";
//    }


    /**
     * 史泽浩
     * 前台登出方法
     *
     * @param request
     * @return
     */
    @RequestMapping("loginout")
    public String frontloginout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("userInfo") != null) {
            session.invalidate();
        }
        return "redirect:/index.jsp";
    }

    /**
     * 史泽浩
     * 注册时手机框失焦验证
     *
     * @param tel
     * @return
     */
    @GetMapping("tel")
    @ResponseBody
    public Message telValidate(String tel) {
        log.info("手机号校验：" + tel);
        // 进来先置空msg，否则还保留上次的值
        msg.setMsg("");
        // n值为2代表符合手机正则
        int n = RegexUtil.validateName(tel);
        if (n != 2) {
            msg.setMsg("手机号格式不正确");
        } else {
            User exchange = us.getUserByTel(tel);
            if (exchange != null) {
                msg.setMsg("该手机号已被注册");
            }
        }
        return msg;
    }

    /**
     * 验证码返回给前端验证，注册和登录通用
     * 史泽浩
     *
     * @param tel
     * @return
     */
    @GetMapping("tel/{tel}")
    @ResponseBody
    public Message returnCodeToFront(@PathVariable("tel") String tel) {
        String code = TelValidator.telValidator(tel);
        // String code = "123456";
        msg.setMsg("");
        msg.setMsg(code);
        return msg;
    }

    /**
     * 史泽浩
     * 手机注册方法
     *
     * @param user
     * @param
     * @return
     */
    @PutMapping("telRegister")
    @ResponseBody
    public Message register(User user, HttpSession session) {
        msg.setMsg("");
        // model.addAttribute("user", createdUser);
        // log.info("注册的User信息：" + user);
        boolean flag = us.insertByTel(new User(user.getTel()));
        User getUser = us.getUserByTel(user.getTel());
        if (flag) {
            msg.setMsg("注册成功");
            if (user.getStatus() == 1) {
                msg.setMsg("注册成功并登录");
                session.setAttribute("userInfo", getUser);
            }
            return msg;
        } else {
            msg.setMsg("注册失败");
            return msg;
        }
    }

    @PostMapping("codeLogin")
    @ResponseBody
    public Message codeLogin(User user, HttpSession session) {
        msg.setMsg("");
        User getUser = us.getUserByTel(user.getTel());
        if (getUser != null) {
            msg.setMsg("登录成功");
            session.setAttribute("userInfo", getUser);
        } else {
            msg.setMsg("登录失败");
        }
        return msg;
    }

    @PostMapping("quicklylogin")
    public String quickLogin(User user, Model model) {
        if (user.getStatus() == 1) {
            User getUser = us.getUserByTel(user.getTel());
            model.addAttribute("userInfo", getUser);
            log.info("去home");
            return "home";
        } else {
            return "frontlogin";
        }
    }
}
